import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:my_flutter_tool/ui/flow_video_test.dart';
import 'package:my_flutter_tool/util/app_util.dart';
import 'package:my_flutter_tool/widgets/app_button.dart';
import 'package:my_flutter_tool/widgets/app_divider.dart';
import 'package:my_flutter_tool/widgets/app_list_tile.dart';
import 'package:my_flutter_tool/widgets/app_text_field.dart';
import 'package:my_flutter_tool/widgets/count_down_btn.dart';

import 'ui/refresh_load_test.dart';
import 'util/log_utils.dart';
import 'widgets/comment/comment_box.dart';
import 'ui/setting_page.dart';
import 'widgets/loaders/dance_loader.dart';
import 'widgets/loaders/hor_loader.dart';
import 'widgets/safe_appbar.dart';
import 'widgets/safe_scaffold.dart';

void main() {
  runApp(const MyApp());

  SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
    statusBarColor: Colors.transparent,
  ));

  L.init(tag: "SILENCE");
}

class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    L.w('rebuild MyApp');

    return GetMaterialApp(
      title: 'Flutter Tool',
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    A.initScreen(context);

    L.w('rebuild MyHomePage');

    return const HomeBody();
  }
}

///主体
class HomeBody extends StatelessWidget {
  const HomeBody({Key key}) : super(key: key);

  ///显示评论区
  _showComment(BuildContext context) async {
    await showModalBottomSheet(
      elevation: 0,
      barrierColor: Colors.black12,
      context: context,
      backgroundColor: Colors.transparent,
      isScrollControlled: true,
      builder: (context) => const CommentBox(),
    );
  }

  ///显示底部弹框
  _showBottomSheet(BuildContext context) async {
    await A.showBottom(context, (context, physics) {
      return ListView.separated(
        itemCount: 100,
        physics: physics,
        itemBuilder: (_, index) => AppListTile(
          title: Text("TITLE"),
          subTitle: Text('content xxxxxxxxxxxx'),
          padding: EdgeInsets.symmetric(vertical: 50.w, horizontal: 40.w),
        ),
        separatorBuilder: (_, __) => AppDivider(),
      );
    });
  }

  ///显示对话框
  _showDialog(BuildContext context) async {
    await A.tips(
      context,
      title: "提示",
      content: "内容XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
      // bottom: true,
      onCancel: () {},
    );
  }

  @override
  Widget build(BuildContext context) {
    L.d('rebuild HomeBody');

    const List<String> _funNames = [
      "显示评论",
      "加载视图",
      "显示底部弹框",
      "视频流",
      "显示对话框",
    ];

    final List<Function> _funs = [
      () => _showComment(context),
      () async {
        await A.jump(context, const RefreshLoadTest());
      },
      () => _showBottomSheet(context),
      () async => await A.jump(context, const FlowVideoTest()),
      () => _showDialog(context),
    ];

    return SafeScaffold(
      appBar: SafeAppBar(
        title: Text('Flutter Tool'),
        actions: [
          IconButton(
            icon: Icon(Icons.settings),
            onPressed: () async {
              A.jump(context, const SettingPage()).then((v) {
                L.i('content value $v');
              });
            },
          ),
        ],
      ),
      body: ListView(
        padding: EdgeInsets.symmetric(horizontal: 80.w, vertical: 60.h),
        physics: A.appPhysics,
        children: [
          AppTextField(),
          const AppDivider(),
          AppTextField(),
          const AppDivider(),
          AppButton(
            text: '取消焦点',
            onPressed: () {
              A.unFocus(context);
            },
          ),
          const AppDivider(),
          Center(
            child: CountDownBtn(
              onTap: () async {
                L.i('发送验证码');
                return true;
              },
            ),
          ),
          const AppDivider(),
          SizedBox(
            height: 100.h,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                AppButton(
                  width: 300.w,
                  text: '隐藏状态栏',
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 40.sp,
                  ),
                  onPressed: () {
                    A.hideStatusBar();
                  },
                ),
                const AppDivider(direction: Axis.vertical),
                AppButton(
                  width: 300.w,
                  text: '显示状态栏',
                  onPressed: () {
                    A.showStatusBar(dark: false);
                  },
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 40.sp,
                  ),
                ),
              ],
            ),
          ),
          const AppDivider(),
          SizedBox(
            height: 100.h,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                const DanceLoader(),
                const AppDivider(direction: Axis.vertical),
                const HorLoader(),
              ],
            ),
          ),
          const AppDivider(),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Container(
                width: 200.w,
                height: 200.h,
                color: Colors.purple,
                child: Text('width: 200.w\nheight: 200.h'),
              ),
              Container(
                width: 200.w,
                height: 200.w,
                color: Colors.purple,
                child: Text('width: 200.w\nheight: 200.w'),
              ),
              Container(
                width: 200.h,
                height: 200.h,
                color: Colors.purple,
                child: Text('width: 200.h\nheight: 200.h'),
              ),
            ],
          ),
          const AppDivider(),
          ListView.separated(
            shrinkWrap: true,
            physics: const NeverScrollableScrollPhysics(),
            itemCount: _funNames.length,
            itemBuilder: (_, index) {
              return AppButton(
                text: _funNames[index],
                onPressed: _funs[index],
              );
            },
            separatorBuilder: (_, __) => const AppDivider(),
          ),
        ],
      ),
    );
  }
}
